# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def pluralize_object_count(count)
    s = "object"
    
    unless count == 1 || count == '1'
      s = s.pluralize
    end
    
    return content_tag("span", count, :class => "object_count") + " " + s
  end


  def resource_title(resource)
    page_title(resource.name  )
  end


  def page_title(title)
    content_tag("h1",title)
  end

  def pluralize_verb_noun(count, verb, noun)
    
    return pluralize_verb(count, verb) + " " + pluralize(count, noun)
  end
  
  def pluralize_noun_verb(count, noun, verb)
    
    return pluralize(count, noun) + " " + pluralize_verb(count, verb)
  end

  def pluralize_verb(count, singular, plural = nil)
    verbs = { 'has' => 'have', 'was' => 'were', 'is' => 'are', 'wants' => 'want' }
    if count == 1
      singular
    elsif plural
      plural
    elsif verbs[singular]
      verbs[singular]
    else
      singular
    end
  end
  
  def link_to_content(name)
    link_to(name, "#body", {:class => "skip_navigation", :accesskey => "2"})
  end

  def link_to_homepage(name, controller = "homepage")
    navigation_link_to(name, {:controller => controller}, {:rel => "home bookmark", :accesskey => "1"})
  end

  def list_link_to(name, options = {}, html_options = {})
    content_tag("li", link_to(name, options, html_options))
  end

  def navigation_link_to(name, options = {}, html_options = {})      
    if current_page?(options)
      content_tag("span", name)
    else
      link_to name, options, html_options
    end
  end

  def google_chart(size = "440x220", options = {})
    options.symbolize_keys!
    
    base_uri = "http://chart.apis.google.com/chart"

    parameters = Array.new

    case options[:type]
      when "map"
        type = "t"
      when "line"
        type = "lc"
      when "pie"
        type = "p"
      when "bar"
        type = "bvs"
      else
        type = "lc"
    end

    
    if options[:type] == "map"
      parameters << "chld=" + options[:country_codes].join
      parameters << "chco=" + options[:country_default_color].to_s + "," + options[:country_min_colour].to_s + "," + options[:country_max_color].to_s
    end
    
    if options[:type] == "pie"
      chl = options[:labels].join("|")
    elsif options[:type] == "bar"
      chl = options[:labels].join("|")
      chds = ""
      chbh = ((440 - (options[:data].size * 4)) / options[:data].size).to_s
    else
    chds = ""
    chxt = ""
    chxl = options[:labels]
    end

    chd = options[:data].join(",")
    
#   cht = chart_type (lc = line_chart, lxy = multiple_line_chart, ls = sparklines...)
#   chl=Hello|World is the chart's label.
#   cht=t:  = chart data string (text_encoding)
#   chds  = chart data scaling    
#   chxl = axis labels
    

    parameters << "chtm=" + options[:map_area] if options[:map_area]
    parameters << "cht=" + type
    parameters << "chs=" + size
    parameters << "chd=" + "t:" + chd if chd
    parameters << "chds=" + "0" + "," + options[:data].sort.last.to_s if chds
    parameters << "chxl=" + "0:|" + options[:labels].first + "|" + options[:labels].last + "|1:||" + options[:data].sort.last.to_s if options[:labels] if chxl
    parameters << "chl=" + chl if chl
    parameters << "chxt=" + "x,y" if chxt
    parameters << "chbh=" + chbh + ",4" if chbh

#    parameters << "chtm=" + area
#    parameters << "chld=" + "GBMGKETN"
#    parameters << "chco" + "ffffff,edf0d4,13390a"
    
    image_path = base_uri + "?" + parameters.join("&")
    
    image_tag(image_path, :alt => "[map]")
  
  end  
    
  
  
end
